//引入fs模块
const fs = require('fs')
//引入path模块
const path = require('path')

//引入http模块
const http = require('http')

//创建http实例
const server = http.createServer()

//监听request事件
server.on('request', (req, res) => {
  //解构获取url
  const { url } = req

  if (url === '/' || url === 'index.html') {
    //读取clock的文件
    fs.readFile(
      path.join(__dirname, '/clock/index.html'),
      'utf-8',
      function (err, dataStr) {
        if (err) return res.end(err)

        res.end(dataStr)
      }
    )
  } else if (url === '/index.css' || url === '/index.js') {
    //读取css和js的内容
    fs.readFile(
      path.join(__dirname, '/clock', url),
      'utf-8',
      function (err, dataStr) {
        if (err) return res.end(err)

        res.end(dataStr)
      }
    )
  } else {
    console.log(url)

    res.end('404 Not Found')
  }
})

//启动服务器
server.listen('8081', () => {
  console.log('My server is running http://localhost:8081')
})
